package poswar.contribute.mbean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import poswar.contribute.exception.QuestionWhereAnsweredException;
import poswar.contribute.model.Answer;
import poswar.contribute.util.FacesUtils;


@ManagedBean
public class AnswerBean {
	private String answer;
	
	@ManagedProperty("#{questionBean}")
	private QuestionBean questionBean;
 
	@ManagedProperty("#{userBean}")
	private UserBean userBean;

	public String publishAnswer() {
		if (FacesUtils.isEmpty(answer)) return null;
		
		Answer answer = Answer.build(userBean.getEmail(), this.answer);
		try {
			questionBean.publishAnswer(answer);
		} catch (QuestionWhereAnsweredException e) {
			FacesUtils.addErrorMsg("It's not allowed to answer the question twice");
		}
		
		return null;
	}
		
	public String getAnswer() {
		return answer;
	}

	public void setAnswer(String answer) {
		this.answer = answer;
	}

	public QuestionBean getQuestionBean() {
		return questionBean;
	}

	public void setQuestionBean(QuestionBean questionBean) {
		this.questionBean = questionBean;
	}

	public UserBean getUserBean() {
		return userBean;
	}

	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}
	
	
}
